﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace GSync
{
    public class GSyncFactory
    {
        protected GSyncFactory()
        { }
        protected static GSyncFactory _Instance;
        public static GSyncFactory Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new GSyncFactory();
                }
                return _Instance;
            }
        }


        public virtual IDataProvider GetLocalContactStorage()
        {
            return new OutlookMobileContactsProvider();
        }

        public virtual IDataProvider GetRemoteContactStorage()
        {
            return new GoogleContactsProvider();
        }


        protected static IHashCodesCacheManager _HashCodesCacheInstance;
        public virtual IHashCodesCacheManager GetHashCodesCache()
        {
            if (_HashCodesCacheInstance == null)
            {
                _HashCodesCacheInstance = new HashCodesCacheMngr();
            }
            return _HashCodesCacheInstance;
        }

        protected ISettingsManager _SettingsManager;
        public virtual ISettingsManager SettingsManager
        {
            get
            {
                if (_SettingsManager == null)
                { 
                    _SettingsManager = new PocketPCSettingsManager();
                    _SettingsManager.Load();
                }
                return _SettingsManager;
            }
        }
        public virtual IConflictResolver GetConflictResolver()
        {
            return new ConflictResolveForm();
        }

        public virtual SyncManager BuildContactsSyncManager()
        {
            SyncManager mngr = new SyncManager();

            mngr.CacheMngr = GetHashCodesCache();
            mngr.ConflictResolver = GetConflictResolver();
            mngr.LocalStorage = GetLocalContactStorage();
            mngr.RemoteStorage = GetRemoteContactStorage();

            return mngr;
        }

        public virtual SyncManager BuildCalendarSyncManager()
        {
            SyncManager mngr = new SyncManager();

            mngr.CacheMngr = GetHashCodesCache();
            mngr.ConflictResolver = GetConflictResolver();
            mngr.LocalStorage = GetLocalCalendarStorage();
            mngr.RemoteStorage = GetRemoteCalendarStorage();

            return mngr;
        }

        public virtual IDataProvider GetLocalCalendarStorage()
        {
            return new OutlookMobileAppointmentsProvider();
        }

        public virtual IDataProvider GetRemoteCalendarStorage()
        {
            return new GoogleAppointmentsProvider();
        }

        protected ILogger _Logger;
        public virtual ILogger Logger
        {
            get
            {
                if (_Logger == null)
                {
                    _Logger = new TextFileLogger(SettingsManager.TraceFile);
                }

                return _Logger;
            }
        }

     

    }
}
